/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.editors;

import faneclipse.views.FanContentOutlinePage;

import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

/**
 * The FanEditor class control the text editor.
 * @author BEGAUDEAU stephane
 */
public class FanEditor extends TextEditor {
	/**
	 * The object containing the attributes of the text.
	 */
	private static FanTextAttributeProvider fanTextAttributeProvider = new FanTextAttributeProvider();
	
	/**
	 * The object in charge of scanning the code.
	 */
	private static FanCodeScanner fanCodeScanner = new FanCodeScanner(fanTextAttributeProvider);
	
	/**
	 * The object in charge of scanning a partition.
	 */
	private static FanPartitionScanner fanPartitionScanner = new FanPartitionScanner();
	
	/**
	 * The ContentOutlinePage.
	 */
	private FanContentOutlinePage fOutlinePage;
	
	/**
	 * The constructor.
	 */
	public FanEditor() {
		super();
	}
	
	/**
	 * This method will allow the editor to inform the outline view.
	 * @param required The class that need an adapter.
	 * @return The adapter returned, here for a IContentOutlinePage we will return a FanContentOutlinePage.
	 * @see org.eclipse.ui.editors.text.TextEditor#getAdapter(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public final Object getAdapter(final Class required) {
		if (IContentOutlinePage.class.equals(required)) {
			if (fOutlinePage == null) {
				fOutlinePage= new FanContentOutlinePage(this);
				if (getEditorInput() != null) {
					fOutlinePage.setInput(getEditorInput());
				}
			}
			return fOutlinePage;
		}
		return super.getAdapter(required);
	}
	
	/**
	 * This method is in charge of the initialization of the editor.
	 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
	 */
	protected final void initializeEditor() {
		setSourceViewerConfiguration(new FanSourceViewerConfiguration());
	}
	
	/**
	 * @return The object in charge of scanning a partition.
	 */
	public static FanPartitionScanner getFanPartitionScanner() {
		return fanPartitionScanner;
	}
	
	/**
	 * @return The object in charge of scanning the code.
	 */
	public static FanCodeScanner getFanCodeScanner() {
		return fanCodeScanner;
	}
	
	/**
	 * @return The object containing the attributes of the text.
	 */
	public static FanTextAttributeProvider getFanTextAttributeProvider() {
		return fanTextAttributeProvider;
	}
}
